/*-
 * Copyright (c) 2012-2013 Shang-Rong Cai. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $ MTBLAS: stblas_1_scl.c    Rev. 0.0.5    2013-10-03T22:20+08:00 $
 */

#include <bscerr.h>

#include <exec_isa.h>
#include <stblas.h>

int stblas_scalf(const size_t sz, const float alpha, float *vx, const step_t incx)
{
    int ierr = ERRNO_UNDEFINED;

    operand_t ra;
    operand_t rb;
    operand_t rc;

    ra.addr = (addr_t) &alpha;
    ra.step = 0;
    rb.addr = (addr_t) vx;
    rb.step = sizeof(float) * incx;
    rc.addr = (addr_t) vx;
    rc.step = sizeof(float) * incx;

    execute((void*) op3_mul_ff, sz, &ra, &rb, &rc, NULL);

    return ierr;
}


int stblas_scal(const size_t sz, const double alpha, double *vx, const step_t incx)
{
    int ierr = ERRNO_UNDEFINED;

    operand_t ra;
    operand_t rb;
    operand_t rc;

    ra.addr = (addr_t) &alpha;
    ra.step = 0;
    rb.addr = (addr_t) vx;
    rb.step = sizeof(double) * incx;
    rc.addr = (addr_t) vx;
    rc.step = sizeof(double) * incx;

    execute((void*) op3_mul_df, sz, &ra, &rb, &rc, NULL);

    return ierr;
}


int stblas_scall(const size_t sz, const long double alpha, long double *vx, const step_t incx)
{
    int ierr = ERRNO_UNDEFINED;

    operand_t ra;
    operand_t rb;
    operand_t rc;

    ra.addr = (addr_t) &alpha;
    ra.step = 0;
    rb.addr = (addr_t) vx;
    rb.step = sizeof(long double) * incx;
    rc.addr = (addr_t) vx;
    rc.step = sizeof(long double) * incx;

    execute((void*) op3_mul_lf, sz, &ra, &rb, &rc, NULL);

    return ierr;
}


int stblas_cscalf(const size_t sz, const float complex alpha, float complex *vx, const step_t incx)
{
    int ierr = ERRNO_UNDEFINED;

    operand_t ra;
    operand_t rb;
    operand_t rc;

    ra.addr = (addr_t) &alpha;
    ra.step = 0;
    rb.addr = (addr_t) vx;
    rb.step = sizeof(float complex) * incx;
    rc.addr = (addr_t) vx;
    rc.step = sizeof(float complex) * incx;

    execute((void*) op3_mul_fc, sz, &ra, &rb, &rc, NULL);

    return ierr;
}


int stblas_cscal(const size_t sz, const double complex alpha, double complex *vx, const step_t incx)
{
    int ierr = ERRNO_UNDEFINED;

    operand_t ra;
    operand_t rb;
    operand_t rc;

    ra.addr = (addr_t) &alpha;
    ra.step = 0;
    rb.addr = (addr_t) vx;
    rb.step = sizeof(double complex) * incx;
    rc.addr = (addr_t) vx;
    rc.step = sizeof(double complex) * incx;

    execute((void*) op3_mul_dc, sz, &ra, &rb, &rc, NULL);

    return ierr;
}


int stblas_cscall(const size_t sz, const long double complex alpha, long double complex *vx, const step_t incx)
{
    int ierr = ERRNO_UNDEFINED;

    operand_t ra;
    operand_t rb;
    operand_t rc;

    ra.addr = (addr_t) &alpha;
    ra.step = 0;
    rb.addr = (addr_t) vx;
    rb.step = sizeof(long double complex) * incx;
    rc.addr = (addr_t) vx;
    rc.step = sizeof(long double complex) * incx;

    execute((void*) op3_mul_lc, sz, &ra, &rb, &rc, NULL);

    return ierr;
}


int stblas_fcscalf(const size_t sz, const float alpha, float complex *vx, const step_t incx)
{
    int ierr = ERRNO_UNDEFINED;

    float complex buf;

    operand_t ra;
    operand_t rb;
    operand_t rc;

    buf = (float complex) alpha;

    ra.addr = (addr_t) &buf;
    ra.step = 0;
    rb.addr = (addr_t) vx;
    rb.step = sizeof(float complex) * incx;
    rc.addr = (addr_t) vx;
    rc.step = sizeof(float complex) * incx;

    execute((void*) op3_mul_fc, sz, &ra, &rb, &rc, NULL);

    return ierr;
}


int stblas_fcscal(const size_t sz, const double alpha, double complex *vx, const step_t incx)
{
    int ierr = ERRNO_UNDEFINED;

    double complex buf;

    operand_t ra;
    operand_t rb;
    operand_t rc;

    buf = (double complex) alpha;

    ra.addr = (addr_t) &buf;
    ra.step = 0;
    rb.addr = (addr_t) vx;
    rb.step = sizeof(double complex) * incx;
    rc.addr = (addr_t) vx;
    rc.step = sizeof(double complex) * incx;

    execute((void*) op3_mul_dc, sz, &ra, &rb, &rc, NULL);

    return ierr;
}


int stblas_fcscall(const size_t sz, const long double alpha, long double complex *vx, const step_t incx)
{
    int ierr = ERRNO_UNDEFINED;

    long double complex buf;

    operand_t ra;
    operand_t rb;
    operand_t rc;

    buf = (long double complex) alpha;

    ra.addr = (addr_t) &buf;
    ra.step = 0;
    rb.addr = (addr_t) vx;
    rb.step = sizeof(long double complex) * incx;
    rc.addr = (addr_t) vx;
    rc.step = sizeof(long double complex) * incx;

    execute((void*) op3_mul_lc, sz, &ra, &rb, &rc, NULL);

    return ierr;
}
